Convert Date to String and String to Date in Java
π Java Date Shenanigans! πβ
Time flies when you're having fun, but in Java, you need some serious tricks to handle it! Let's dive into the magical world of java.util.Date
and learn how to create, format, parse, and manipulate dates like a true time wizard! π§ββοΈ
β³ 1. Formatting a Date to Stringβ
Java's SimpleDateFormat
lets you turn a Date
into a beautifully formatted string. But beware! It's not thread-safe, so don't go sharing it recklessly. π±
SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy");
String date = sdf.format(new Date());
π Common Date Patternsβ
Pattern | Example |
---|---|
yyyy-MM-dd (ISO) | "2018-07-14" |
dd-MMM-yyyy | "14-Jul-2018" |
dd/MM/yyyy | "14/07/2018" |
E, MMM dd yyyy | "Sat, Jul 14 2018" |
h:mm a | "12:08 PM" |
yyyy-MM-dd'T'HH:mm:ssZ | "2018-07-14T14:31:30+0530" |
π 2. Parsing a String to Dateβ
Ever received a date as a string and wondered how to turn it into a real date? SimpleDateFormat.parse()
to the rescue! π¦ΈββοΈ
SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
String dateInString = "15-10-2015 10:20:56";
Date date = sdf.parse(dateInString);
β° 3. Getting Current Date and Timeβ
Want to know what time it is? Java's Date
class has your back! π
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));
Since Java 8, we have fancier options like LocalDate
and LocalTime
:
LocalDate today = LocalDate.now();
System.out.println("Today's Local date : " + today);
LocalTime time = LocalTime.now();
System.out.println("Local time now : " + time);
π 4. Convert between Date and Calendarβ
π 4.1. Calendar to Dateβ
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
π° 4.2. Date to Calendarβ
SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
String dateInString = "27-04-2016 10:22:56";
Date date = sdf.parse(dateInString);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
βοΈ 5. Comparing Two Datesβ
Time to settle the "who's older" debate! The compareTo()
method can tell if one date is before, after, or the same as another. π€
Date date1 = new Date();
Date date2 = new Date();
int comparison = date1.compareTo(date2);
Result | Meaning |
---|---|
0 | Both dates are identical! π€ |
< 0 | date1 is before date2 βͺ |
> 0 | date1 is after date2 β© |
π 6. Extracting Days, Months, and Yearsβ
Need to pull apart a date and get specific components? Calendar
has you covered! ποΈ
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH); // Jan = 0, not 1 π²
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
π Conclusionβ
Javaβs date handling might seem tricky at first, but once you get the hang of it, youβll be a Date-Time Ninja! π₯· Keep practicing, and remember: Never trust SimpleDateFormat
in a multithreaded environment! π
Happy Learning & Happy Coding! π